import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';

export default {
    namespace: 'headline',
    state: {
        headline: {},
        headlines: { totalCount: 0, items: [] },
        headlineSearchs: { pageIndex: 1, pageSize, keywords: "" }
    },
    effects: {
        /**
         * 获取今日头条列表
         * @param param0 
         * @param param1 
         */
        *getHeadlines({ payload }, { call, put }) {
            const { keywords, pageIndex, pageSize } = payload;
            const formSerchKey = keywords, formSkipCount = (pageIndex - 1) * pageSize, formMaxResultCount = pageSize;
            const headlines: api.SingleUserMallHeadLineVOHeadlinVO =
                yield request(api.ManageheadLinesManageApiFetchParamCreactor.manageheadLinesManageGetAllHeadLinesAsync
                    ({
                        formSerchKey, formSkipCount, formMaxResultCount
                    }))
            yield put({ type: 'save', payload: { headlines, headlineSearchs: { keywords, pageIndex, pageSize } } });
        },
        /**
         * 获取今日头条详情
         * @param param0 
         * @param param1 
         */
        *getHeadline({ payload }, { call, put }) {
            const articleId = payload.id;
            const headline: api.SingleUserMallHeadLineVOHeadlineDetailVO =
                yield request(api.ManageheadLinesManageApiFetchParamCreactor.manageheadLinesManageGetArticleDetailAsync({
                    articleId,
                }))
            yield put({ type: 'save', payload: { headline } });
        },
        /**
         * 保存今日头条
         * @param param0 
         * @param param1 
         */
        *saveHeadline({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const { headlineSearchs } = yield select(({ headline }) => headline);
            yield request(api.ManageheadLinesManageApiFetchParamCreactor.manageheadLinesManageSaevHeadlineAsync(
                { form }
            ));
            yield put({ type: 'getHeadlines', payload: { ...headlineSearchs } });
            callback && callback(true);
        },
         /**
         * 删除今日头条
         * @param param0 
         * @param param1 
         */
        *remove({ payload }, { call, put, select }) {
            const { form, callback } = payload;
            const articleId=payload.form.id;
            const { headlineSearchs } = yield select(({ headline }) => headline);
            yield request(api.ManageheadLinesManageApiFetchParamCreactor.manageheadLinesManageDeleteArticleAsync(
                { articleId }
            ));
            yield put({ type: 'getHeadlines', payload: { ...headlineSearchs } });
            callback && callback(true);
        }
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
        },
    },
};